﻿using SelfMadeMillionaire.Core.Entities;
using SelfMadeMillionaire.Core.Enums;
using SelfMadeMillionaire.Core.Services;
using SelfMadeMillionaire.Web.Infrastructure.Controllers;
using SelfMadeMillionaire.Web.Infrastructure.ViewModels.Publications;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using PagedList;
using SelfMadeMillionaire.Core.Specifications.Publications;
using SelfMadeMillionaire.Web.Infrastructure.ViewModels.Home;
using SelfMadeMillionaire.Core.Specifications.Newss;

namespace SelfMadeMillionaire.Web.Infrastructure.Controllers
{
	public class PublicationsController : BaseController
	{
		readonly IPublicationService _publicationService;
        readonly INewsService _newsService;
		readonly ISubscriberService _subscriberService;

        public PublicationsController(IPublicationService publicationService, ISubscriberService subscriberService, INewsService newsService)
		{
			_publicationService = publicationService;
			_subscriberService = subscriberService;
            _newsService = newsService;
		}


		public ActionResult Index(DateTime? startDate, DateTime? endDate, ProductType? selectedProduct, string title, int? page, string sort)
		{
            ClearProductTypeInSession();

			int pageIndex = page ?? 1;
			int pageSize = 10;

			var productType = selectedProduct.HasValue ? selectedProduct.Value : GetProductTypeDepenenOnReferrer(selectedProduct);

            startDate = startDate ?? new DateTime(DateTime.Today.Year, 1, 1);
            endDate = endDate ?? new DateTime(DateTime.Today.Year, 12, 31);

            PublicationNewsListViewModel tModel = new PublicationNewsListViewModel();
            tModel.Publications = _publicationService.GetList(new PublicationsListSpecification(startDate, endDate, productType, title, false, GetMinAccessLevel())).ToList();
            tModel.News = _newsService.GetList(new NewsListSpecification(startDate, endDate, productType, title, GetMinAccessLevel(), null)).ToList();
            tModel.CreateMixedPagedList(pageIndex, pageSize, sort);

            var model = new PublicationAndNewsListViewModel
			{                
				Publications = tModel.MixedPagedList,
				StartDate = startDate,
				EndDate = endDate,
				ProductTypes = GetProductTypes(productType),
				SelectedProduct = productType.HasValue ? productType.Value : ProductType.None,
				Title = title,
                SortOrder = sort
			};

			if (Request.IsAjaxRequest())
				return PartialView("PublicationsList", model);

			return View(model);
		}

		[HttpGet]
		public ActionResult Details(int id)
		{
			var model = _publicationService.Get(id);

            if (model.MinAccessLevel > (byte)GetMinAccessLevel())
            {
                return new RedirectResult("Index");
            }

			Session[Constants.PRODUCT_TYPE_SESSION_KEY] = model.ProductType;
			return View(model);
		}

		private SubscriptionType GetMinAccessLevel()
		{
			var minAccessLevel = SubscriptionType.Free;
			if (Request.IsAuthenticated)
				minAccessLevel = _subscriberService.GetMinAccessLevel(User.Identity.Name);

			return minAccessLevel;
		}
	}
}
